# DjangoDE, an integrated development environment for Django
# Copyright (C) 2010-2011 Andrew Wilkinson
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import os
import sys
import unittest

from djangode import global_objects
from djangode.gui import MainWindow
from djangode.project import Project
from djangode.utils import config
from djangode.utils.qt import Qt, QtCore, QtGui

class TestLeftBarSettings(unittest.TestCase):
    def setUp(self):
        if hasattr(sys, "last_type"):
            del sys.last_type

        global_objects.main_window = MainWindow()
        global_objects.main_window.show()

        global_objects.project = Project(os.path.dirname(__file__) + os.sep + "test_project" + os.sep + "manage.py")
        global_objects.main_window.reload_project()
        self.process_events()

    def tearDown(self):
        global_objects.main_window.close()
        global_objects.main_window = None

        global_objects.project = None

        config.CONFIG.clear() # Don't listen to the configuration for the user who is running the tests.
        config.CONFIG.sync()

    def test_settings_listed(self):
        self.assert_(global_objects.main_window.left_bar.settings_pane.settings.count() > 0)

    def test_click_setting(self):
        item = global_objects.main_window.left_bar.settings_pane.settings.item(0)
        global_objects.main_window.left_bar.settings_pane.settings.setCurrentItem(item)
        self.process_events()

        global_objects.main_window.left_bar.settings_pane.settings.itemDoubleClicked.emit(item)
        self.process_events()

        self.assert_(not hasattr(sys, "last_type")) # Check that there were no exceptions while processing events.

        settings_open = False
        for file_name in global_objects.main_window.editor.open_files.keys():
            if "settings.py" in file_name:
                settings_open = True

        self.assert_(settings_open)

    def test_click_open_setting(self):
        global_objects.main_window.left_bar.settings_pane.open_settings_button.clicked[bool].emit(True)
        self.process_events()

        self.assert_(not hasattr(sys, "last_type")) # Check that there were no exceptions while processing events.

        settings_open = False
        for file_name in global_objects.main_window.editor.open_files.keys():
            if "settings.py" in file_name:
                settings_open = True

        self.assert_(settings_open)

    def process_events(self):
        while global_objects.app.hasPendingEvents():
            global_objects.app.processEvents()
